package com.watson.juc;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author watson
 * @date 2021/8/2 14:53
 * @Description: 死锁
 **/
public class LocksDemo4 {
    public static void main(String[] args) {
        String lockA = "lockA";
        String lockB = "lockB";
        new Thread(new MyThread(lockA, lockB), "T1").start();
        new Thread(new MyThread(lockB, lockA), "T2").start();
    }


}


class MyThread implements Runnable {
    private String lockA;
    private String lockB;

    public MyThread(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }

    @Override
    public void run() {
        synchronized (lockA) {
            System.out.println(Thread.currentThread().getName() + "lock:" + lockA + ",=>get:" + lockB);
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (lockB) {
                System.out.println(Thread.currentThread().getName() + "lock:" + lockB + ",=>get:" + lockA);
            }
        }
    }
}


class dad {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Thread01 t = new Thread01();
        t.start();
        Thread02 t1 = new Thread02();
        new Thread(t1).start();
        FutureTask futureTask = new FutureTask<>(new Thread03());
        new Thread(futureTask).start();
        System.out.println(futureTask.get());
    }

}

class Thread01 extends Thread {
    @Override
    public void run() {
        System.out.println("11");
    }
}

class Thread02 implements Runnable {
    @Override
    public void run() {
        System.out.println("121");
    }
}

class Thread03 implements Callable {
    @Override
    public Object call() throws Exception {
        return "hello";
    }
}
